(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.loan.logController", logController);
	logController.$inject = [ "$scope", "$q", "$timeout", "modalService", "layoutConfig", "validatorConfig", "authService", "loanService" ];
	function logController($scope, $q, $timeout, modalService, layoutConfig, validatorConfig, authService, loanService) {
		var ctrl = this, errorMessage = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.loan = {};

		ctrl.init = function() {
			loanService.createInitOptionData().then(function(initData) {
				return $q(function(resolved, rejected) {
					ctrl.loan.prodCodeOptions = initData.prodCode;
					ctrl.loan.assetCcyOptions = initData.currency;
					ctrl.loan.prodCategoryOptions = initData.prodCategory;
					ctrl.loan.countryOptions = initData.country;
					ctrl.loan.communityIdOptions = initData.communityIdOptions;
					resolved();
				});
			}).then(function() {
				ctrl.layoutConfig = loanService.deleteDynaparm(layoutConfig);
				authService.logInit().then(function(response) {
					ctrl.loan = angular.merge(ctrl.loan, response.data || {});
				});
			});
		};

		ctrl.prodCodeChanged = function(loan, formCtrl, isInitialized) {
			if (isInitialized) {
				return;
			}
			if (loan && loan.loanInfo && loan.loanInfo.prodCode) {
				var param = {
					"prodCode" : loan.loanInfo.prodCode
				};
				loanService.getProdCodeChangedData(param).then(function(prodCodeChangedData) {
					return $q(function(resolved, rejected) {
						ctrl.loan.ccyOptions = prodCodeChangedData.ccyOptions;
						ctrl.loan.assetTypeOptions = prodCodeChangedData.assetTypeOptions;
						ctrl.loan.loanInfo.prodType = prodCodeChangedData.prodType;
						ctrl.loan.loanInfo.prodSubtype = prodCodeChangedData.prodSubType;
						ctrl.loan.loanInfo.product = prodCodeChangedData.product;
						resolved();
					});
				}).then(function() {
					loanService.getLayout(ctrl.loan.loanInfo.prodType, ctrl.loan.loanInfo.prodSubtype, "loan", "log", false).then(function(layout) {
						ctrl.layoutConfig = layout || layoutConfig;
						var tmp = angular.copy(ctrl.loan);
						$timeout(function() {
							ctrl.loan = tmp;
						});
					});
				});
			}
		};

		ctrl.dateChanged = function(loan) {
			loanService.dateChanged(loan);
		};
		ctrl.tenorDaysChanged = function(loan) {
			loanService.tenorDaysChanged(loan);
		};
		
		ctrl.communityIdChanged = function(loan){
			var loanInfo = loan.loanInfo;
			if(loanInfo && loanInfo.communityId){
				loanService.getApCommunity(loanInfo.communityId).then(function(map){
					if(map && map.company){
						loanInfo.obligorCode = map.company.companyCode;
						loanInfo.obligorName = map.company.companyName;
						loanInfo.obligorAddr1 = map.company.companyAddr1;
						loanInfo.obligorAddr2 = map.company.companyAddr2;
						loanInfo.obligorAddr3 = map.company.companyAddr3;
						loanInfo.obligorAddr4 = map.company.companyAddr4;
						loanInfo.obligorCity = map.company.companyCity;
						loanInfo.obligorZip = map.company.companyZip;
						loanInfo.obligorState = map.company.companyState;
						loanInfo.obligorCountry = map.company.companyCountry;
					}
				});
			}
		};

		ctrl.logSubmitted = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			} else {
				return authService.logSubmitted(ctrl.loan).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}
	}
})(angular);
